package com.cavy.platform.server.service;

import com.cavy.platform.server.domain.Project;
import com.cavy.platform.server.dto.ProjectCreateDto;
import com.cavy.platform.server.dto.ProjectDeleteDto;
import com.cavy.platform.server.dto.ProjectDto;
import com.cavy.platform.server.dto.ProjectSearchDto;
import com.cavy.platform.server.mapper.ProjectMapper;
import com.cavy.platform.server.repository.ProjectRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 唐旭
 * @Date 2018/7/17
 */
@Service
public class ProjectService {
    @Autowired
    private ProjectRepository repository;

    public void create(ProjectCreateDto dto) {
        Project project = ProjectMapper.INSTANCE.convertTo(dto);
        repository.create(project);
    }

    public void delete(ProjectDeleteDto dto) {
        repository.delete(ProjectMapper.INSTANCE.convertTo(dto));
    }

    public List<ProjectDto> list(ProjectSearchDto dto) {
        List<Project> projects = repository.list(dto.getName(), dto.getWorkSpace());
        List<ProjectDto> list = new ArrayList<>(projects.size());
        for (Project item : projects) {
            list.add(ProjectMapper.INSTANCE.convertToDto(item));
        }
        return list;
    }
}
